﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ufida.u9.ait.openapi.utils;
using UFIDA.U9.Base;
using UFIDA.U9.CBO.HR.Department;
using UFIDA.U9.CBO.HR.Operator;
using UFIDA.U9.CBO.SCM.Customer;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.CBO.SCM.ProjectTask;
using UFIDA.U9.SM.ForecastOrder;
using UFSoft.UBF.Business;

namespace ufida.u9.ait.openapi
{
    internal class ForecastOrderBase
    {
        public static ForecastOrder UnApproveDoc(ForecastOrder doc)
        {
            if (doc.Status != ForecastOrderStatusEnum.Approved)
            {
                throw new Exception($"单号[{doc.DocNo}]状态为[{doc.Status.Name}],不能弃审");
            }

            using (ISession session = Session.Open())
            {
                doc.Status = ForecastOrderStatusEnum.Opened;
                doc.ApproveDate = DateTime.MinValue;
                doc.Approver = "";
                doc.UnApprover = Context.LoginUser;
                doc.UnApproveDate= DateTime.MinValue;
                foreach(var line in doc.ForecastOrderLines)
                {
                    line.Status= ForecastOrderStatusEnum.Opened;
                }
                session.Commit();
            }

            return doc;
        }

        public static ForecastOrder ApproveDoc(ForecastOrder doc)
        {
            if (doc.Status == ForecastOrderStatusEnum.Approved
                || doc.Status == ForecastOrderStatusEnum.Closed)
            {
                throw new Exception($"单号[{doc.DocNo}]状态为[{doc.Status.Name}],不能审批");
            }

            using (ISession session = Session.Open())
            {
                doc.Status = ForecastOrderStatusEnum.Approved;
                doc.ApproveDate = DateTime.Now;
                doc.Approver = Context.LoginUser;
                foreach(var line in doc.ForecastOrderLines)
                {
                    line.Status= ForecastOrderStatusEnum.Approved;
                }
                session.Commit();
            }

            return doc;
        }

        public static ForecastOrder CreateDoc(ForecastOrderAITDTOData dto)
        {
            ForecastOrder doc = null;
            //创建实体
            using (ISession session = Session.Open())
            {
                doc = CreateHead(dto);
                CreateLine(doc, dto);

                session.Commit();
            }

            return doc;
        }

        private static void CreateLine(ForecastOrder doc, ForecastOrderAITDTOData dto)
        {
            int i = 0;
            foreach (var linedto in dto.ForecastOrderLines)
            {
                //校验
                if (linedto.ItemInfo == null)
                {
                    throw new Exception("ItemInfo为空");
                }
                if (linedto.Num <= 0)
                {
                    throw new Exception("Num为空");
                }

                //赋值
                ForecastOrderLine line = ForecastOrderLine.Create(doc);
                i++;
                if (linedto.DocLineNo <= 0)
                {
                    line.DocLineNo = i * 10;
                }
                else
                {
                    line.DocLineNo = linedto.DocLineNo;
                }

                //料品
                ItemMaster item = EntityUtils.GetEntityByCode<ItemMaster>(linedto.ItemInfo.ItemCode);
                line.ItemInfo = new ItemInfo();
                line.ItemInfo.ItemID = item;
                line.ItemInfo.ItemCode = item.Code;
                line.ItemInfo.ItemName = item.Name;
                line.TU = item.SalesUOM;
                line.Num = linedto.Num;

                //客户
                if (doc.Customer != null&&doc.Customer.Customer!=null)
                {
                    line.Customer = new CustomerMISCInfo();
                    line.Customer.Customer = doc.Customer.Customer;
                    line.Customer.Code = doc.Customer.Code;
                    line.Customer.Name = doc.Customer.Name;
                    if (doc.Customer.Customer.CustomerSites.Count <= 0)
                        throw new Exception($"客户[{doc.Customer.Customer.Code}]没有客户位置");
                    line.ShipToSite = new CustomerSiteMISCInfo();
                    line.ShipToSite.CustomerSite = doc.Customer.Customer.CustomerSites[0];
                    line.ShipToSite.Code = doc.Customer.Customer.CustomerSites[0].Code;
                    line.ShipToSite.Name = doc.Customer.Customer.CustomerSites[0].Name;
                }
                
                //日期
                if(!String.IsNullOrEmpty(linedto.ShipPlanDate))
                    line.ShipPlanDate=DateTime.Parse(linedto.ShipPlanDate);
                else if(doc.ShipPlanDate!=null)
                    line.ShipPlanDate = doc.ShipPlanDate;

                //扩展字段
                if (linedto.DescFlexField != null)
                {
                    line.DescFlexField = new UFIDA.U9.Base.FlexField.DescFlexField.DescFlexSegments();
                    DTOUtils.DTOPropCopy(linedto.DescFlexField, line.DescFlexField);
                }

                //其他
                line.SupplyOrg = doc.Org;
                line.ShiperOrg = doc.Org;
                line.OrderOperator = doc.OrderOperator;
                line.Department = doc.Department;
                line.Note = linedto.Note;

            }
        }

        private static ForecastOrder CreateHead(ForecastOrderAITDTOData dto)
        {
            ForecastOrder doc = ForecastOrder.Create();
            //校验
            if (dto.DocmentType == null)
            {
                throw new Exception("DocmentType为空");
            }
            //if (dto.Customer == null)
            //{
            //    throw new Exception("Customer为空");
            //}
            if (String.IsNullOrEmpty(dto.BusinessDate))
            {
                throw new Exception("BusinessDate为空");
            }
            //赋值
            doc.Org = Context.LoginOrg;
            doc.OrderOperator = EntityUtils.GetEntityByCode<Operators>(dto.OrderOperator);
            doc.DocmentType = EntityUtils.GetEntityByCode<ForecastOrderDocType>(dto.DocmentType);
            doc.Department = EntityUtils.GetEntityByCode<Department>(dto.Department);
            doc.Project = EntityUtils.GetEntityByCode<Project>(dto.Project);
            //客户
            if(dto.Customer != null)
            {
                doc.Customer = new UFIDA.U9.CBO.SCM.Customer.CustomerMISCInfo();
                Customer cus = EntityUtils.GetEntityByCode<Customer>(dto.Customer);
                doc.Customer.Customer = cus;
                doc.Customer.Code = cus.Code;
                doc.Customer.Name = cus.Name;
            }
            //日期处理
            doc.BusinessDate = DateTime.Parse(dto.BusinessDate);
            if (String.IsNullOrEmpty(dto.ShipPlanDate))
                doc.ShipPlanDate = doc.BusinessDate;
            else
                doc.ShipPlanDate = DateTime.Parse(dto.ShipPlanDate);
            //扩展字段
            if(dto.DescFlexField!= null)
            {
                doc.DescFlexField = new UFIDA.U9.Base.FlexField.DescFlexField.DescFlexSegments();
                DTOUtils.DTOPropCopy(dto.DescFlexField,doc.DescFlexField);
            }
            //其他
            doc.WorkFlowID = Guid.Empty;
            doc.WFOriginalState = 1;
            doc.WFCurrentState = 2;
            doc.StateMachineID = Guid.Empty;
            doc.Note= dto.Note;

            return doc;
        }
    }
}
